package com.zxjbyte.yiyi.module.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zxjbyte.yiyi.framework.common.domain.api.PageResult;
import com.zxjbyte.yiyi.framework.common.domain.api.ResultCode;
import com.zxjbyte.yiyi.framework.common.exception.ServiceException;
import com.zxjbyte.yiyi.framework.common.util.AuthUtil;
import com.zxjbyte.yiyi.framework.common.util.ValidationUtil;
import com.zxjbyte.yiyi.framework.mybatis.service.impl.BaseServiceXImpl;
import com.zxjbyte.yiyi.framework.mybatis.util.Condition;
import com.zxjbyte.yiyi.module.system.convert.ErrorLogConvert;
import com.zxjbyte.yiyi.module.system.domain.dto.ErrorLogAddDTO;
import com.zxjbyte.yiyi.module.system.domain.entity.ErrorLogPO;
import com.zxjbyte.yiyi.module.system.domain.param.ErrorLogListParam;
import com.zxjbyte.yiyi.module.system.domain.param.ErrorLogProcessParam;
import com.zxjbyte.yiyi.module.system.domain.vo.ErrorLogListVO;
import com.zxjbyte.yiyi.module.system.mapper.ErrorLogMapper;
import com.zxjbyte.yiyi.module.system.service.ErrorLogService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.zxjbyte.yiyi.framework.common.enums.SortOrder.DESC;


/**
 * @Author zhangxingjia
 * @Date 2023/3/17 17:31
 * @Version: 1.0
 */
@Service
public class ErrorLogServiceImpl extends BaseServiceXImpl<ErrorLogMapper, ErrorLogPO> implements ErrorLogService {

    @Override
    public PageResult<ErrorLogListVO> list(ErrorLogListParam param) {
        LambdaQueryWrapper<ErrorLogPO> wrapper = Wrappers.<ErrorLogPO>lambdaQuery().ge(Objects.nonNull(param.getStartExTime()), ErrorLogPO::getExceptionTime, param.getStartExTime())
                .le(Objects.nonNull(param.getEndExTime()), ErrorLogPO::getExceptionTime, param.getEndExTime())
                .eq(Objects.nonNull(param.getProcessStatus()), ErrorLogPO::getProcessStatus, param.getProcessStatus());
        IPage<ErrorLogPO> page = page(Condition.getPage(param, ErrorLogPO::getCreateTime, DESC), wrapper);
        List<ErrorLogListVO> voList = ErrorLogConvert.INSTANCE.logPoListToVoList(page.getRecords());
        return PageResult.of(voList, page);
    }

    @Override
    public void process(ErrorLogProcessParam param) {
        ValidationUtil.assertFalse(param.getId() <= 0 || Objects.isNull(getById(param.getId())), () -> new ServiceException(ResultCode.PARAM_VALID_ERROR));
        lambdaUpdate().set(ErrorLogPO::getProcessStatus, param.getProcessStatus())
                .set(ErrorLogPO::getProcessTime, new Date())
                .set(ErrorLogPO::getProcessUserId, AuthUtil.getUserId())
                .set(ErrorLogPO::getProcessRemarks, param.getProcessRemarks())
                .eq(ErrorLogPO::getId, param.getId()).update();
    }

    @Override
    public void add(ErrorLogAddDTO logAddDto) {
        save(ErrorLogConvert.INSTANCE.logAddDtoToPo(logAddDto));
    }
}
